{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "import matplotlib.font_manager as fm\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从EXCEL表中导入数据\n",
    "examDf = pd.read_csv(\"time-score.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学习时间</th>\n",
       "      <th>学习成绩</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.50</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.75</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.00</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.25</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.50</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.75</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.75</td>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2.00</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2.25</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2.50</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2.75</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>3.00</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>3.25</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>3.50</td>\n",
       "      <td>62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>4.00</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>4.25</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>4.50</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>4.75</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5.00</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>5.50</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>6.00</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    学习时间  学习成绩\n",
       "0   0.50    10\n",
       "1   0.75    22\n",
       "2   1.00    13\n",
       "3   1.25    43\n",
       "4   1.50    20\n",
       "5   1.75    22\n",
       "6   1.75    23\n",
       "7   2.00    33\n",
       "8   2.25    50\n",
       "9   2.50    62\n",
       "10  2.75    48\n",
       "11  3.00    55\n",
       "12  3.25    75\n",
       "13  3.50    62\n",
       "14  4.00    73\n",
       "15  4.25    81\n",
       "16  4.50    76\n",
       "17  4.75    64\n",
       "18  5.00    82\n",
       "19  5.50    90\n",
       "20  6.00    93"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "examDf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、提取特征和标签\n",
    "exam_X = examDf.loc[:, '学习时间']\n",
    "exam_Y = examDf.loc[:, '学习成绩']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABIwAAAHuCAYAAAD9UpdZAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAn2UlEQVR4nO3da7BlaVkn+P9zKqsoTgFSQIIFVOZBB7yMbQNmoIIXWpARRaAjGgfd0CXSfWIiGBrsFhtNu2mnO9WxEUGix4hDIZayRRlUoG1FGIQWpkN6sqAQsAyRtjJBi6riolwOt6Ke+bB2Vp2V5u1UnX32ufx+ERnvfp+19t7PyVgfTv7zXe+q7g4AAAAAnLK06AYAAAAA2FkERgAAAACMCIwAAAAAGBEYAQAAADAiMAIAAABgRGAEAAAAwIjACADY06rqwFnqF1XVQzbMH3ra8W882+dV1SM3zB9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5DknyY5doZjP9zdtwdJVfVNSZ6R5Ftm+yAlyY3d/bIk6e5fn5331Rk21P6vp33exbPz3j8b16vqXlX1S0k+tnU/EgDA5tSG33kAANik2RPWDnT3357nvOrTfvGqqgOnrVQ6Vb+ku794eh0AYLsIjAAAAAAYOf0+egAAAAD2OYERAAAAACMCIwAAAABGBEYAAAAAjAiMAAAAABj5/wFea9IeoECNIgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 1440x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 2、建立训练数据和测试数据\n",
    "\n",
    "# 这个主要是为了显示中文\n",
    "myfont = fm.FontProperties(fname=r'C:\\Windows\\Fonts\\simsun.ttc')\n",
    "plt.figure(figsize=(20,8))\n",
    "plt.scatter(exam_X,exam_Y,color='red',label='学习成绩')\n",
    "plt.title(\"原始数据图形\", fontproperties = myfont)\n",
    "plt.xlabel('学习时间',fontproperties = myfont)\n",
    "plt.ylabel('学习成绩',fontproperties = myfont)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#3、通过散点图发现学习时间与考试成绩符合线性回归的模式，为了构建模型先要建立训练数据和测试数据\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(exam_X,exam_Y,train_size=0.8)\n",
    "print('原始数据特征：',exam_X.shape,\n",
    "      '训练数据特征:',X_train.shape,\n",
    "      '测试数据特征：',X_test.shape)\n",
    "\n",
    "print('原始数据标签：',exam_Y.shape,\n",
    "      '训练数据标签：',y_train.shape,\n",
    "      '测试数据标签：',y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4、接着描绘训练数据和测试数据图像：\n",
    "# 散点图\n",
    "\n",
    "plt.scatter(X_train,y_train,label='训练数据')\n",
    "plt.scatter(X_test,y_test,label='测试数据')\n",
    "plt.legend(loc=2,prop=myfont)\n",
    "plt.title(\"分数随着学习时间分布\", fontproperties = myfont)\n",
    "plt.xlabel('时间',fontproperties = myfont)\n",
    "plt.ylabel('分数',fontproperties = myfont)\n",
    "# 显示图像\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5、构建线性回归模型\n",
    "# sklearn要求输入的 特征必须是二维数组的类型，但是因为我们目前只有一个特征，\n",
    "# 所以需要用array.reshape(-1,1)转成二维数组的类型，reshape行的参数是-1，就会根据所给的列数，\n",
    "# 自动按照原始数组的大小形成一个新的的数组\n",
    "#将训练数据特征转换成二维数组XX行*1列形式\n",
    "X_train = X_train.values.reshape(-1,1)\n",
    "y_train = y_train.values.reshape(-1,1)\n",
    "\n",
    "#创建线性回归模型\n",
    "model = LinearRegression()\n",
    "\n",
    "\n",
    "\n",
    "# 训练模型\n",
    "model.fit(X_train,y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "# # # 保存模型数据\n",
    "output_model_file = 'saved_model.pkl'\n",
    "with open(output_model_file,'wb') as f:\n",
    "    pickle.dump(model,f)\n",
    "\n",
    "\n",
    "# # 读取模型数据\n",
    "output_model_file = 'saved_model.pkl'\n",
    "with open(output_model_file,'rb') as f:\n",
    "    model =pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 6、找出最佳拟合线\n",
    "#\n",
    "# # 截距\n",
    "a = model.intercept_\n",
    "# # 回归系数\n",
    "b= model.coef_\n",
    "print('最佳拟合线：截距a=',a,'回归系数b=',b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 7、绘制最佳拟合线的图像\n",
    "# # 训练数据散点图\n",
    "plt.scatter(X_train,y_train,label='训练数据')\n",
    "# 训练数据预测值\n",
    "y_train_pred = model.predict(X_train)\n",
    "# 绘制最佳拟合线\n",
    "plt.plot(X_train,y_train_pred,color='green',linewidth=3,label='最佳拟合线')\n",
    "plt.legend(loc=2,prop=myfont)\n",
    "plt.xlabel('时间',fontproperties = myfont)\n",
    "plt.ylabel('得分',fontproperties = myfont)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 8、构建相关系数矩阵\n",
    "rdf = examDf.corr()\n",
    "print('相关系数矩阵：',rdf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 9、求出决定系数R平方\n",
    "X_test = X_test.values.reshape(-1,1)\n",
    "y_test = y_test.values.reshape(-1,1)\n",
    "score = model.score(X_test,y_test)\n",
    "print('决定系数R平方:',score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 10、通过测试数据进行评估模型\n",
    "# 绘制最佳拟合线的图像\n",
    "# 训练数据散点图\n",
    "plt.scatter(X_train,y_train,color='blue',label='训练数据')\n",
    "# 训练数据预测值\n",
    "y_train_pred = model.predict(X_train)\n",
    "# 绘制最佳拟合线\n",
    "plt.plot(X_train,y_train_pred,color='green',linewidth=3,label='最佳拟合线')\n",
    "# 测试数据散点图\n",
    "plt.scatter(X_test,y_test,color='red',label='测试数据')\n",
    "plt.legend(loc=2,prop=myfont)\n",
    "plt.xlabel('时间',fontproperties = myfont)\n",
    "plt.ylabel('得分',fontproperties = myfont)\n",
    "\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
